package com.shensi.adminCollect.controller.system;

import com.shensi.adminCollect.common.utils.poi.ExcelUtil;
import com.shensi.adminCollect.domain.system.SysAgent;
import com.shensi.adminCollect.service.system.ISysAgentService;
import com.shensi.adminCollect.service.system.ISysUserService;
import com.shensi.common.core.annotation.Log;
import com.shensi.common.core.controller.BaseController;
import com.shensi.common.core.domain.AjaxResult;
import com.shensi.common.core.domain.entity.SysUser;
import com.shensi.common.core.enums.BusinessType;
import com.shensi.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 代理机构Controller
 *
 * @author qujia
 * @date 2021-07-01
 */
@RestController
@RequestMapping("/system/agent")
public class SysAgentController extends BaseController
{
    @Autowired
    private ISysAgentService sysAgentService;
    @Autowired
    private ISysUserService sysUserService;
    /**
     * 查询代理机构列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysAgent sysAgent)
    {
        startPage();
        List<SysAgent> list = sysAgentService.selectSysAgentList(sysAgent);
        return getDataTable(list);
    }

    /**
     * 导出代理机构列表
     */
    @Log(title = "代理机构", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysAgent sysAgent)
    {
        List<SysAgent> list = sysAgentService.selectSysAgentList(sysAgent);
        ExcelUtil<SysAgent> util = new ExcelUtil<SysAgent>(SysAgent.class);
        return util.exportExcel(list, "agent");
    }

    /**
     * 获取代理机构详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(sysAgentService.selectSysAgentById(id));
    }

    /**
     * 新增代理机构
     */
    @Log(title = "代理机构", businessType = BusinessType.INSERT)
    @PostMapping("/addSysAgent")
    public AjaxResult addSysAgent(@RequestBody SysAgent sysAgent)
    {
        List<Integer> sysAgentlist =new ArrayList<>();
        try {
            if(sysAgent!=null && sysAgent.getFlag()!=null && sysAgent.getFlag().equals("1")){
                if(sysAgent.getList()!=null &&sysAgent.getList().size()>0 && sysAgent.getUserId()!=null){
                    SysUser user = sysUserService.selectUserById(sysAgent.getUserId());
                    if(user.getAgentFlag()!=null &&user.getAgentFlag().equals("1")){
                        for(String group : sysAgent.getList()){
                            SysAgent agent = new SysAgent();
                            agent.setGroupId(Long.valueOf(group));
                            agent.setUserId(sysAgent.getUserId());
                            sysAgentService.insertSysAgent(agent);
                        }
                    }
                }
            }else if(sysAgent!=null && sysAgent.getFlag()!=null && sysAgent.getFlag().equals("0")){
                if(sysAgent.getList()!=null &&sysAgent.getList().size()>0 && sysAgent.getUserId()!=null){
                    for(String group : sysAgent.getList()){
                        SysAgent agent = new SysAgent();
                        agent.setGroupId(Long.valueOf(group));
                        agent.setUserId(sysAgent.getUserId());
                        SysAgent sysAgent1 =  sysAgentService.selectSysAgent(agent);
                        if (sysAgent1!=null) {
                             sysAgentService.deleteSysAgentById(sysAgent1.getId().intValue());
                        }
                    }
                }
            }
           sysAgentlist = sysAgentService.sysAgentlist(sysAgent);
        }catch (Exception e){
            System.out.println("------------------------------------"+e.getLocalizedMessage()+"");
            e.printStackTrace();
        }
        return AjaxResult.success(sysAgentlist);
    }

    /**
     * 查询代理已存在的
     */
    @GetMapping("/sysAgentlist")
    public AjaxResult sysAgentlist(SysAgent sysAgent)
    {
        return  AjaxResult.success(sysAgentService.sysAgentlist(sysAgent));
    }


    /**
     * 修改代理机构
     */
    @Log(title = "代理机构", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysAgent sysAgent)
    {
        return toAjax(sysAgentService.updateSysAgent(sysAgent));
    }

    /**
     * 删除代理机构
     */
    @Log(title = "代理机构", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(sysAgentService.deleteSysAgentByIds(ids));
    }
}
